package com.android.jianboshi.ui.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.jianboshi.R;
import com.android.jianboshi.constants.Constants;


/**
 * 低电量，维修
 */
public class Tips_View extends LinearLayout {
    public Tips_View(Context context) {
        super(context);
        this.context = context;
        initView(null);
    }

    public Tips_View(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    public Tips_View(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    Context context;
    public ImageView iv_device, iv_low_battery;
    boolean isLow_battery = false;

    private void initView(AttributeSet attrs) {
        View.inflate(context, R.layout.view_tips, this);
        iv_device = findViewById(R.id.iv_device);
        iv_low_battery = findViewById(R.id.iv_low_battery);

    }

    //解析传过来的数据
    public void setData(byte[] data) {
        byte byte1 = data[11 + Constants.length];  //是否故障  0x00为正常工作模式，0x01 为标定/校准 模式，0x02为睡眠模式，0x03为异常模式(包括打气打300秒达不到4.50PSI；气压1分钟内速降到0.5PSI)
        if (byte1 == (byte) 0x03)  //异常
        {
            iv_device.setVisibility(View.VISIBLE);   //异常就显示工具图标
        } else {
            iv_device.setVisibility(View.GONE);     //否则影藏
        }
        byte byte2 = data[17 + Constants.length];    //低电时值为0，非低电时值为0x64
        if (byte2 == (byte) 0x00)  //低电量
        {
            if(isLow_battery==false){
                anim();      //是低电量就开始闪
            }
            isLow_battery = true;
        } else {
            isLow_battery = false;
            handler.removeCallbacks(runnable_gone);  //否则隐藏图标
            iv_low_battery.setVisibility(View.GONE);
        }
    }

    int animTime = 500;
    Handler handler = new Handler();

    private void anim() {
        iv_low_battery.setVisibility(View.VISIBLE);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                iv_low_battery.setVisibility(View.GONE);
                handler.postDelayed(runnable_gone, animTime);
            }
        }, animTime);
    }

    Runnable runnable_gone = new Runnable() {
        @Override
        public void run() {
            anim();
        }
    };

}
